package com.lighting.smssystem.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lighting.smssystem.aspect.RequireRole;
import com.lighting.smssystem.constrants.UserRole;
import com.lighting.smssystem.model.Metric;
import com.lighting.smssystem.model.Record;
import com.lighting.smssystem.model.User;
import com.lighting.smssystem.model.bo.UserList;
import com.lighting.smssystem.service.IUserService;
import com.lighting.smssystem.utils.BaseResponse;
import com.lighting.smssystem.utils.ResponseUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Author LeeMaster
 *
 * 授权新的管理员，冻结锁定用户，调整用户短信发送限制，开通用户vip
 */
@RestController
@RequestMapping("/admin")
@Api(
        value = "管理员信息接口",
        protocols = "http",
        description = "管理平台用户和管理员相关接口"
)
@Slf4j
public class AdminController {


    @Resource
    private IUserService userService;

    @ApiOperation(
            value = "管理员列表",
            httpMethod = "GET",
            authorizations = @Authorization(
                    "ADMIN 用户"
            )

    )
    @GetMapping("")
    @RequireRole(role = UserRole.ADMIN)
    public BaseResponse listAdmin(
            @ApiParam @RequestParam(value = "pageSize",defaultValue = "10") Integer limit,
            @ApiParam @RequestParam(value = "pageNum",defaultValue = "1") Integer offset
    ){
        log.info("查询管理员列表");
        return ResponseUtil.success(
                userService.listAdmin(limit,offset)
        );
    }

    @ApiOperation(
            value = "创建管理员用户",
            httpMethod = "POST",
            authorizations = @Authorization(
                    "ADMIN 用户"
            )

    )
    @PostMapping("")
    @RequireRole(role = UserRole.ADMIN)
    public BaseResponse addAdmin(
            @ApiParam @RequestBody User user
            ){
        log.info("创建管理员列表{}", user.toString());
        userService.createAdminUser(user);
        return  ResponseUtil.success();
    }

    @ApiOperation(
            value = "删除管理员权限",
            httpMethod = "DELETE",
            authorizations = @Authorization(
                    "ADMIN 用户"
            )

    )
    @DeleteMapping("")
    @RequireRole(role = UserRole.ADMIN)
    public BaseResponse deleteAdmin(
            @ApiParam(name = "管理员账户ID") @RequestParam("id") Long adminId
    ){
        log.info("删除管理员{}", adminId);
        return ResponseUtil.success(
                userService.revokeAdmin(adminId)
        );
    }

    @ApiOperation(
            value = "用户列表",
            httpMethod = "GET",
            authorizations = @Authorization(
                    "ADMIN 用户"
            )

    )
    @ApiResponse(code = 200,message = "",response = UserList.class)
    @GetMapping("/user")
    @RequireRole(role = UserRole.ADMIN)
    public BaseResponse listUsers(
            @ApiParam @RequestParam(value = "pageSize",defaultValue = "10") Integer limit,
            @ApiParam @RequestParam(value = "pageNum",defaultValue = "1") Integer offset
    ){
        log.info("查询用户列表");
        return ResponseUtil.success(
                userService.listUsers(limit,offset)
        );
    }


    @ApiOperation(
            value = "更新用户短信限制和剩余条数",
            httpMethod = "PUT",
            authorizations = @Authorization(
                    "ADMIN 用户"
            )

    )
    @PutMapping("/metric")
    @RequireRole(role = UserRole.ADMIN)
    public BaseResponse updateUserMetric(
            @ApiParam @RequestBody Metric metric
            ){
        log.info("修改用户用量{}", metric.toString());
        return ResponseUtil.success(userService.updateMetric(metric));
    }

    @ApiOperation(
            value = "开通用户vip",
            httpMethod = "PUT",
            authorizations = @Authorization(
                    "ADMIN 用户"
            )

    )
    @PutMapping("/vip")
    @RequireRole(role = UserRole.ADMIN)
    public BaseResponse openUserVip(
            @ApiParam @RequestParam("id") Long id
    ){
        log.info("开通用户Vip{}", id);

        return ResponseUtil.success(userService.activationVip(id));
    }



}
